import axios from 'axios'
import { ElMessage } from 'element-plus';

// 创建axios实例对象
var service = axios.create({
    baseURL: 'https://localhost:5001/',
    timeout: 5000,
});

// 请求拦截器
service.interceptors.request.use(config => {
    //携带token
    let token = window.localStorage.getItem('token');
    if (token) {
        config.headers.Authorization = 'Bearer ' + token;
    }
    return config;
}, rror => {
    Promise.reject(rror); // 对请求错误做些什么
});

// 响应拦截器
service.interceptors.response.use(response => {
    if (response.status === 200 || response.status === 201) {
        return response;
    }
}, error => {
    let message = '';
    const status = error.status;
    switch (status) {
        case 400:
            message = "请求参数错误";
            break;
        case 401:
            message = "TOKEN过期";
            break;
        case 403:
            message = "无权访问";
            break;
        case 404:
            // message = "请求地址错误";
            message = "未找到有关数据";
            break;
        case 500:
            message = "服务器出现问题";
            break;
        default:
            message = "网络出现问题";
            break;
    }
    ElMessage.error(message)
    return error;
})

export default service;